package com.wt.struggle.goodbye.main.fragment

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.support.v4.app.ActivityCompat
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.ProV4Fragment
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.main.activity.RecordVideoActivity
import com.wt.struggle.goodbye.main.activity.SendTieActivity
import com.wt.struggle.goodbye.view.CustomPop
import com.xin.lv.yang.utils.utils.FileUtils
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.video_fragment_layout.*

/**
 * 视频选择或者录制
 */
class VideoFragment : ProV4Fragment() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

    }

    override fun createView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.video_fragment_layout, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        imageVideoAdd.setOnClickListener {

            val code1 = ActivityCompat.checkSelfPermission(activity!!, Manifest.permission.READ_EXTERNAL_STORAGE)
            val code2 = ActivityCompat.checkSelfPermission(activity!!, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            val code3 = ActivityCompat.checkSelfPermission(activity!!, Manifest.permission.CAMERA)

            if (code1 == PackageManager.PERMISSION_GRANTED &&
                code2 == PackageManager.PERMISSION_GRANTED &&
                code3 == PackageManager.PERMISSION_GRANTED
            ) {

                showChoose()

            } else {

                ActivityCompat.requestPermissions(
                    activity!!,
                    arrayOf(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA
                    ), 6666
                )

            }
        }

    }

    /**
     * 选择录制视频的弹窗
     */
    private fun showChoose() {
        val customPop = CustomPop(activity!!, 2)

        setAlpha(activity!!, 0.6f)
        customPop.show(object : CustomPop.OnCheckListener {

            override fun onCamera() {

                val intent = Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI)

                intent.type = "video/*"

                startActivityForResult(intent, 789)
                customPop.dismiss()
                setAlpha(activity!!, 1f)
            }

            override fun onPhoto() {
                /// 录制视频
                val intent = Intent(MediaStore.ACTION_VIDEO_CAPTURE)
                startActivityForResult(intent, 789)
                customPop.dismiss()
                setAlpha(activity!!, 1f)
            }

            override fun onClose() {
                customPop.dismiss()
                setAlpha(activity!!, 1f)
            }

        })


    }


    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 6666) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                grantResults[1] == PackageManager.PERMISSION_GRANTED &&
                grantResults[2] == PackageManager.PERMISSION_GRANTED
            ) {
                showChoose()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        log("执行到获取视频详情的位置------")

        if (requestCode == 789 && resultCode == Activity.RESULT_OK) {

            if (data != null) {

                val videoUrl = data.data

                Log.i("result", "--------$videoUrl")

                val cr = activity!!.contentResolver

                val cursor = cr.query(videoUrl, null, null, null, null)

                if (cursor != null && cursor.moveToNext()) {

                    Log.i("result", "--------111---$videoUrl")

                    val videoPath: String = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))

                    (activity as SendTieActivity).saveVideo(Uri.parse(videoPath))

                    cursor.close()

                } else {
                    (activity as SendTieActivity).saveVideo(videoUrl)
                }

            }
        }
    }
}